<template>
  <div v-show="show"
       :style="[styleSheet]"
       :class="[dropdownClass,'animate',{'dropUp': dropUp}]">
    <slot></slot>
  </div>
</template>

<script type="text/javascript">
export default {
  name: "VDropdown",
  props: {
    position: {
      type: String,
      default: "left"
    }
  },
  data() {
    return {
      show: false,
      styleSheet: { top: "", left: "" },
      dropdownClass: "v-dropdown-container",
      dropUp: false,
      lastCaller: null,
      lastCallerHeight: -1,
      clientHeight: false
    };
  },
  watch: {
  },
  mounted() {
    this.MouseEventPolyfill();
    this.$on("show", this.visible);
    this.$on("adjust", this.adjust);
    document.addEventListener("mousedown", this.whole); // 鼠标点击事件监听
  },
  destroyed() {
    this.$off("show", this.visible);
    this.$off("adjust", this.adjust);
    document.removeEventListener("mousedown", this.whole);
  },
  methods: {
    visible(state, caller) {
      if (typeof state === "boolean" && this.show !== state) {
        this.show = state;
        const that = this;
        this.$nextTick(() => {
          if (that.show) {
            that.adjust(caller);
            that.lastCaller = caller;
          } else this.$emit("show-change", false);
        });
      }
    },
    adjust(caller) {
      const that = this;
      setTimeout(function() {
        let pos = caller.getBoundingClientRect();
        // let gap = 5;
        let t = 35;
        let l = 0;
        // let up = false;

        let menuPos = that.$el.getBoundingClientRect();
        let screenScrollTop = window.scrollY;
        let viewHeight = document.body.clientHeight;

        // const visibBox = false;

        if (that.rightClick) {
          var top = that.y;
          if (that.y + menuPos.height > screenScrollTop + viewHeight) { top = that.y - menuPos.height; }
          l = that.x;
          t = top;
        } else {
          that.clientHeight = that.$el.offsetHeight;
          if (that.clientHeight + pos.top + pos.height > viewHeight) { // 可视区域小于内容区域
            t = -that.clientHeight + (viewHeight - pos.top);
          }
        }
        that.styleSheet.top = t + "px";
        that.styleSheet.left = l + "px";
        that.lastCallerHeight = pos.height;
      }, 50);
    },
    whole(e) {
      // 任何鼠标事件
      const that = this;
      if (this.show) {
        const idx = e.path.findIndex(
          val =>
            val.className &&
            (val.className.includes(that.dropdownClass) ||
              val.outerHTML === that.lastCaller.outerHTML)
        );
        if (idx === -1) {
          this.show = false;
          this.$emit("show-change", false);
        }
      }
    },
    MouseEventPolyfill() {
      // 初次加载运行
      if (!("path" in Event.prototype)) {
        Object.defineProperty(Event.prototype, "path", {
          get: function() {
            const path = [];
            let currentElem = this.target;
            while (currentElem) {
              path.push(currentElem);
              currentElem = currentElem.parentElement;
            }
            if (path.indexOf(window) === -1 && path.indexOf(document) === -1) {
              path.push(document);
            }
            if (path.indexOf(window) === -1) path.push(window);
            return path;
          }
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
div.v-dropdown-container {
  border: 1px solid #d6d7d7;
  margin: 0;
  padding: 0;
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
  vertical-align: middle;
  box-sizing: border-box;

  /*background-color: #F5F5F5;*/
  background-color: white;
  border-radius: 2px;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
  z-index: 100;
  &.sm_regular {
    width: auto;
    min-width: 150px;
  }
  &.sm_embed {
    position: relative;
  }
  /* Select only */
  & > .sm_select_ng {
    background: #fcc;
  }
  /*输入框设置了input-block-level样式时的特殊情况修复*/
  input.sm_input.input-block-level {
    box-sizing: border-box;
    height: 30px;
    line-height: 30px;
    min-height: 30px;
    width: 100%;
  }
  &.animate {
    -webkit-animation: dropDownFadeInDown 300ms cubic-bezier(0.23, 1, 0.32, 1);
    animation: dropDownFadeInDown 300ms cubic-bezier(0.23, 1, 0.32, 1);
    &.dropUp {
      -webkit-animation: dropDownFadeInUp 300ms cubic-bezier(0.23, 1, 0.32, 1);
      animation: dropDownFadeInUp 300ms cubic-bezier(0.23, 1, 0.32, 1);
    }
  }
}

@keyframes dropDownFadeInDown {
  from {
    opacity: 0;
    transform: translate3d(0, -20px, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}
@keyframes dropDownFadeInUp {
  from {
    opacity: 0;
    transform: translate3d(0, 20px, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}
</style>
